﻿using LVYAN.Utils;
using System;
using System.Collections.Generic;

namespace LVYAN.Service.AIAPI
{

    public class CacheManage<T>
    {
        private static Dictionary<string, T> Dic = new Dictionary<string, T> { };
        private static Dictionary<string, System.DateTime> DicTime = new Dictionary<string, System.DateTime> { };
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="t"></param>
        /// <param name="Minute">默认5分钟</param>
        /// <returns></returns>
        public static T GetCache(string Key, Func<T> t, double Minute = 5)
        {
            Key = new SignService().SignMD5(Key);
            if (DicTime.ContainsKey(Key) && DicTime[Key] > DateTime.Now.AddSeconds(10)) { return Dic[Key]; };
            if (DicTime.ContainsKey(Key))
            {
                DicTime[Key] = System.DateTime.Now.AddMinutes(Minute);
                Dic[Key] = t();
            }
            else
            {
                DicTime.Add(Key, System.DateTime.Now.AddMinutes(Minute));
                Dic.Add(Key, t());
            }
            return Dic[Key];
        }
    }
}
